package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.dto.InheritorsDTO;
import com.sky.entity.Inheritors;
import com.sky.result.Result;
import com.sky.service.impl.InheritorsServiceImpl;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/inheritors")
@Api(tags = "管理端传承人相关接口")
public class InheritorsController {

    @Autowired
    InheritorsServiceImpl inheritorsService;

    /**
     * 新增/修改 传承人信息
     */
    @PostMapping
    public Result<String> post(@RequestBody InheritorsDTO inheritorsDTO) {
        Inheritors inheritors =  new Inheritors();
        BeanUtils.copyProperties(inheritorsDTO,inheritors);
        boolean bool= inheritorsService.saveOrUpdate(inheritors);
        return bool ? Result.success():Result.error("操作失败");
    }

    /**
     * 删除 传承人信息
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<String> ids) {
        inheritorsService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查询 传承人信息
     */
    @GetMapping("/{id}")
    public Result<Inheritors> get(@PathVariable String id) {
        Inheritors inheritors = inheritorsService.getById(id);
        return Result.success(inheritors);
    }

    /**
     * 根据非遗id查询 传承人信息
     */
    @GetMapping
    public Result<List<Inheritors>> getAll(Integer id) {
        return Result.success(inheritorsService.getByNonheritage(id));
    }

}
